
const { PersonModel, GoodsModel } = require('../db/model')


const add = async (req, res) => {
    const { id, goods_id } = req.body
    console.log(req.body);
    const users = await PersonModel.findById(id, { cart: 1 })
    const cart = users.cart
    const item = await GoodsModel.findOne({ goods_id:goods_id})

    if (!item) return 0

    const goods = cart.find(item => item.goods_id == goods_id)

    if (!goods) {
        item.cart_number = 1
        delete item.goods_introduce
        cart.push(item)
    } else {
        goods.cart_number++
    }
    const cart2 = await PersonModel.updateOne({ _id: id }, { cart: cart })

    res.send({
        code: 1,
        message: '添加购物车成功',
        cart: cart2
    })
}

const list = async (req, res) => {
    const { id } = req.query
    const users = await PersonModel.findById(id, { cart: 1 })
    const cart = users.cart
    res.send({
        code: 1,
        message: '购物车列表请求成功',
        cart: cart
    })
}
const clear = async (req, res) => {
    const { id } = req.query
    await PersonModel.updateMany(
        {
            _id: id
        },
        {
            'cart': []
        }
    )
    res.send({
        code: 1,
        message: '购物车列表请求成功',
        cart: cart
    })
}
const selectAll = async (req, res) => {
    let { id, type } = req.query
    type = type == 1 ? true : false
    await PersonModel.updateMany(
        {
            _id: id
        },
        {
            $set: {
                'cart.$[].is_select': type
            }
        }
    )
    res.send({
        code: 1,
        message: '购物车全选成功'
    })
}
const remove = async (req, res) => {
    const { id, goodsId } = req.query
    const users = await PersonModel.findById(id, { cart: 1 })
    const cart = users.cart
    const goods = cart.filter(item => item.goods_id != goodsId)

    await PersonModel.updateOne({ _id: id }, { cart: goods })
    res.send({
        code: 1,
        message: '购物车移除成功'
    })
}
const number = async (req, res) => {
    let { id, goodsId, number } = req.body
    goodsId = goodsId - 0
    number = number - 0
    const users = await PersonModel.findById(id, { cart: 1 })
    const cart = users.cart
    const item = cart.find(item => item.goods_id === goodsId)
    if (number > item.goods_number) return 2
    item.cart_number = number

    await PersonModel.updateOne(
        {
            _id: id,
            'cart.goods_id': goodsId
        },
        {
            $set: {
                'cart.$.cart_number': item.cart_number
            }
        }
    )

    res.send({
        code: 1,
        message: '变更成功'
    })
}
const select = async (req, res) => {
    let { id, goodsId } = req.query
    goodsId = goodsId - 0
    const users = await PersonModel.findById(id, { cart: 1 })
    const cart = users.cart
    const item = cart.find(item => item.goods_id === goodsId)

    item.is_select = !(item.is_select)
    await PersonModel.updateOne(
        {
            _id: id,
            'cart.goods_id': goodsId
        },
        {
            $set: {
                'cart.$.is_select': item.is_select
            }
        }
    )

    res.send({
        code: 1,
        message: '变更成功'
    })
}
const clearcompleted = async (req, res) => {
    const { id } = req.query
    const users = await PersonModel.findById(id, { cart: 1 })
    const cart = users.cart

    const goods = cart.filter(item => item.is_select != true)

    await PersonModel.updateMany(
        {
            _id: id
        },
        {
            'cart': goods
        }
    )

    res.send({
        code: 1,
        message: '变更成功'
    })
}
module.exports = {
    add,
    list,
    selectAll,
    remove,
    number,
    select,
    clear,
    clearcompleted
}